import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.LinkedList;

/**
 * Classe Controleur
 * @author BRUNIAS Michael 
 * @author HAMMAMI Christophe 
 * @author LEOUTRE Thibaut
 */

public class Controleur {
	
	private static Chargement c = new Chargement();
	private static Sauvegarde s = new Sauvegarde();
	private LinkedList<Actions> actions;
    private static Utilisateur u = null;
	
	public void demarage(){		
		actions = new LinkedList<Actions>();
		Telechargement t = new Telechargement("http://fr.old.finance.yahoo.com/d/quotes.csv?s=@%5EFCHI&f=snl1d1t1c1ohgv&e=.csv","actions.txt");
		this.miseAJourDonnees();
		this.majHistorique();
	}
	
	private Actions chargerActions(String nom) {
		Actions a = c.ChargerAction(nom);
		return a;
	}

	public void enregistreActions(){
		while (actions.size()!=0){
			s.Sauvegarde(actions.getFirst());
			actions.removeFirst();
		}
	}
	
	public void enregistreUtil(){
		s.Sauvegarde(u);
	}
	
	public void miseAJourDonnees(){
		try{
			InputStream ips=new FileInputStream("temp"+File.separator+"actions.txt"); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			
			while ((ligne=br.readLine())!=null){
				int i = 0;
				String noma = "";
				String id = "";
				String adr = "";
				while (ligne.charAt(i)!=';'){
						id+=ligne.charAt(i);	
						i++;
				}
				i++;
				while (ligne.charAt(i)!=';'){
					noma+=ligne.charAt(i);	
					i++;
				} 
				noma += ".txt";
				adr = ("http://download.finance.yahoo.com/d/quotes.csv?s="+id+"&f=sl1d1t1c1ohgv&e=.csv");
				Telechargement t;
				t = new Telechargement(adr, noma);
				this.creationFichiersActions(noma, id);
			}
			br.close();
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
	}
	
	public void nouvelUtilisateur(String nom, int n){
		File myFile = new File ("Utilisateurs"+File.separator+nom);
		u = new Utilisateur(nom, n);
	}
	
	public void majHistorique(){
		try{
			InputStream ips=new FileInputStream("temp"+File.separator+"actions.txt"); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			
			while ((ligne=br.readLine())!=null){
				int i = 0;
				String noma = "";
				String id = "";
				String adr = "";
				while (ligne.charAt(i)!=';'){
						id+=ligne.charAt(i);	
						i++;
				}
				i++;
				while (ligne.charAt(i)!=';'){
					noma+=ligne.charAt(i);	
					i++;
				} 
				noma += "h.txt";
				Date d = new Date();
				String m = String.valueOf(d.getMonth()+1);
				String j = String.valueOf(d.getDate());
				adr = ("http://ichart.yahoo.com/table.csv?s="+id+"&d="+m+"&e="+j+"&f=2009&g=d&a=0&b=1&c=2009&ignore=.csv");
				Telechargement t;
				t = new Telechargement(adr, noma);
				
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
	}
	
	public void connexion(String nom){
		String connex = "";
		File myFile = new File ("Utilisateurs"+File.separator+nom);
		if (myFile.exists ()){
			u = c.Chargementutil(nom);
			connex = "ok";
		}
	}
	
	public void creationFichiersActions(String noma, String id){
		try{
			Actions a;
			String n = (id+".serial");
			File myFile = new File ("Actions"+File.separator+n);
			System.out.println(id);
			
			if (myFile.exists()){
				a = chargerActions(id);
				actions.addFirst(a);
				System.out.println(a.retourneId());
			}else{
				InputStream ips2=new FileInputStream("temp"+File.separator+noma); 
				InputStreamReader ipsr2=new InputStreamReader(ips2);
				BufferedReader br2=new BufferedReader(ipsr2);
				String ligne2;
				
				while ((ligne2=br2.readLine())!=null){
					int j = 0;
					double val = 0;
					String v = "";
					while (ligne2.charAt(j)!=','){
						j++;
					}
					j++;
					while (ligne2.charAt(j)!=','){
						v+=ligne2.charAt(j);
						j++;
					}
					val = Double.valueOf(v);
					a = new Actions (id, noma, val);
					actions.addFirst(a);
					System.out.println(a.retourneValeur());
				}
			}
		}catch(Exception e){
			System.out.println(e.toString());
		}
	}
	
	public String ActionsPossedees(Utilisateur u){
		LinkedList<Actions> possede = new LinkedList<Actions>();
		String p = "";
		possede = u.retourneNomAction();
		for (int i=0; i<possede.size(); i++){
			p+= u.retourneNombreAction(possede.get(i));
		}
		return p;
	}
	
	public String HistoriqueDeLaction(Actions a){
		LinkedList<Date> dates = new LinkedList<Date>();
		String p = "";
		dates = a.retourneDate();
		for (int i=0; i<dates.size(); i++){
			p+= a.retourneHistorique(dates.get(i));
		}
		return p;
	}
}